package com.sgcc.pda.mdrh.log;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by cj on 2020/7/3.
 * 平台app日志存储工具类，使用yyyy-MM-dd命名文件夹，存储在Android内部文件位置，具体日志分类存储，用枚举LogType表示。
 * 使用方法：HzLogSaveUtil.saveLog("获取UID成功", HzLogSaveUtil.LogType.LOGIN);
 * 可以在代码关键地方埋点存储日志，便于现场问题排查。
 */
public class HzLogSaveUtil {

    //日志每行起始时间
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss  ", Locale.getDefault());
    //文件夹命名格式
    private static SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    //日志存储的文件夹路径
    //TODO
    private static String logSavePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/"+ "com.sgcc.pda.mdrh" +"/files/log/" + format2.format(new Date());


    public static void saveLog(String content, LogType logType) {
//        if (!BuildConfig.ISDEBUG) return;
        File logPath = new File(logSavePath);
        if (!logPath.exists()) {
            logPath.mkdirs();
        }
        File file = new File(logPath, logType.getName());
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file, true);
            String time = format.format(new Date());
            fileWriter.write(time + getStackTrace(content) + "\r\n");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("cj", "日志存储失败:" + e.getMessage());
        } finally {
            try {
                if (fileWriter != null) {
                    fileWriter.flush();
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static String getStackTrace(String content) {
        Throwable throwable = new Throwable();
        //LogUtil.LOG()的depth是0, LogUtil.e()函数的depth是1, 调用者的depth是2,调用者的调用者是3
        int methodDepth = 2;
        StackTraceElement[] stackTraceElements = throwable.getStackTrace();
        if (methodDepth < stackTraceElements.length) {
            StackTraceElement element = stackTraceElements[methodDepth];
            if (element != null && element.getFileName() != null) {
//                String className = element.getFileName().substring(0, element.getFileName().lastIndexOf("."));
                content = String.format(Locale.getDefault(), "[(%s:%d).%s()] %s", element.getFileName(), element.getLineNumber(), element.getMethodName(), content);
            }
        }
        return content;
    }


    public enum LogType {

        //"登录流程日志"
        LOGIN("login.txt"),
        MQTT("mqtt.txt"),
        WORKORDER("workorder.txt"),
        DATABASE("database.txt");

        private String name;

        LogType(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

}

